package com.mjk.common.util;


import com.mjk.common.tools.logger.BizLogger;

import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import java.awt.print.PrinterJob;
import java.util.HashMap;
import java.util.Map;
/**
 * 打印工具类
 * @author Administrator
 *
 */
public class PrintUtils {

	public static void main(String[] args) {
		getPrints();
	}
  public static void getPrints(){
	  try{
		  PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
	      DocFlavor flavor = DocFlavor.BYTE_ARRAY.PNG;
	//    DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
	
	      //可用的打印机列表(字符串数组)
	      PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, pras);
	      Map<String, PrintService> printMap = new HashMap<>();
          for (PrintService service : printService) {
              String serviceName = service.getName();
              printMap.put(serviceName, service);
              BizLogger.info("serviceName=" + serviceName);

              DocFlavor[] docs = service.getSupportedDocFlavors();
              if (docs != null) {
                  for (DocFlavor doc : docs) {
                      BizLogger.info(serviceName + "支持类型:" + doc);
                  }
              }
          }
	      BizLogger.info("printMap = "+printMap);
	      //当前默认打印机
	      PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
	      String defaaultName = ps.getName();
	      //默认打印机名称
	     BizLogger.info("defaaultName=" + defaaultName);
	
	
	//      PrintService printService1 = printMap.get("58mm Series Printer");
	      PrintService printService1 = printMap.get("NPIA7349C (HP LaserJet Pro MFP M226dn)");
	      BizLogger.info("printService1=" + printService1);
	      PrinterJob printerJob = PrinterJob.getPrinterJob();
	      printerJob.setPrintService(printService1);
	      
	  }catch(Exception e){
		  BizLogger.info(e);
	  }
  }
}
